package com.stu.covid.business.controller;

import com.alibaba.excel.EasyExcel;
import com.stu.covid.business.entity.Clazz;
import com.stu.covid.business.entity.StudentHealth;
import com.stu.covid.business.entity.User;
import com.stu.covid.business.mapper.StudentHealthMapper;
import com.stu.covid.business.service.ClazzService;
import com.stu.covid.business.service.StudentHealthService;
import com.stu.covid.business.utils.DateUtils;
import com.stu.covid.business.utils.Result;
import com.stu.covid.business.utils.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


@Controller
@Slf4j
public class StudentHealthController {
    @Autowired
    private StudentHealthService studentHealthService;

    @Autowired
    private ClazzService clazzService;

    @GetMapping("/to-student-health-list")
    public String toList(Model model) {
        List<Clazz> clazzList = clazzService.all();
        model.addAttribute("clazzList", clazzList);
        return "student_health_list";
    }

    @GetMapping("/to-student-health-add")
    public String toAdd(Model model) {
        User loginUser = SystemUtils.getLoginUser();
        if(loginUser.getStudent() != null){
            Clazz clazz = clazzService.selectById(loginUser.getStudent().getClazzId());
            model.addAttribute("studentName", loginUser.getStudent().getName());
            model.addAttribute("studentNo", loginUser.getStudent().getStudentNo());
            model.addAttribute("clazzName", clazz.getName());
            model.addAttribute("clazzId", clazz.getId());
            model.addAttribute("healthDate", DateUtils.formatDate(new Date(), DateUtils.FORMAT2));
        }
        return "student_health_add";
    }

    @RequestMapping("/student-health-list")
    @ResponseBody
    public Result<StudentHealth> list(StudentHealth studentHealth) {
        log.info("student-health-list:{}", studentHealth);
        return studentHealthService.list(studentHealth);
    }

    @RequestMapping("/student-health-add")
    public String add(StudentHealth studentHealth) {
        studentHealthService.add(studentHealth);
        return "redirect:to-student-health-list";
    }


    @RequestMapping("/student-health-del")
    public String del(StudentHealth studentHealth) {
        studentHealthService.del(studentHealth.getId());
        return "redirect:to-student-health-list";
    }

    @RequestMapping("/student-health-export")
    public void export(HttpServletRequest request, HttpServletResponse response, StudentHealth studentHealth) {
        log.info("student-health:{}", studentHealth);
        studentHealthService.export(request, response,studentHealth);
    }

}
